<?php
/**
 * Created by PhpStorm.
 * User: zhangxilu
 * Date: 2017/12/19
 * Time: 下午11:12
 */

namespace backend\controllers;
use Yii;
use common\models\AdverttypeModel;
use common\models\AdvertModel;
use common\logic\AdvertLogic;
use common\logic\AdverttypeLogic;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\helpers\ArrayHelper;

class AdverttypeController extends BaseController
{
	public function actionIndex()
    {
        
		$query = AdverttypeModel::find();
        $query->orderBy(['ph_adverttype.tid'=>SORT_DESC]);
        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
				
		$dataProvider->sort = false;		
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }
	
    public function actionCreate()
    {
        $model = new AdverttypeModel();
        $model->tid = 0;
        $model->typename = '';
		$model->msg='';
        $post = Yii::$app->request->post();
		
		if(isset($post['AdverttypeModel']) && isset($post['AdverttypeModel']['typename'])){
			$res = AdverttypeLogic::instance()->getTypeByName($post['AdverttypeModel']['typename']);
			if(count($res)>0){
				$model->load($post);
				return $this->render('create', [
	                'model' => $model,
	                'error'=>'类型名字重复'
	            ]);
			}
		}
		
		
        if ($model->load($post) && AdverttypeLogic::instance()->AdverttypeSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
                'error'	=> 'ok'
            ]);
        }
    }
	
    /**
     * Deletes an existing HouseModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param integer $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
    	
		$nums = AdvertLogic::instance()->getPcAdvNum($id);
		if($nums > 0){
//			return $this->redirect('index',['error'=>'改类型已经被使用']);
//			exit("<script>alert('改类型已经被使用');</script>");
//			return $this->error('该类型已经被使用');
			exit(json_encode(['error'=>'该类型已经被使用']));
		}
		
        $this->findModel($id)->delete();
        AdverttypeModel::deleteAll(['tid'=>$id]);
		
        return $this->redirect(['index']);
    }
    
    /**
     * Finds the HouseModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     *
     * @param integer $id
     *
     * @return HouseModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AdverttypeModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    public function actionUpdate($id)
    {
        
		 $model = $this->findModel($id);
		 $post = Yii::$app->request->post();
		
		if(isset($post['AdverttypeModel']) && isset($post['AdverttypeModel']['typename'])){
			$tid = $post['AdverttypeModel']['tid'];
			$res = AdverttypeLogic::instance()->getTypeByNameId($post['AdverttypeModel']['typename'],$tid);
			if(count($res)>0){
				$model->load($post);
				return $this->render('create', [
	                'model' => $model,
	                'error'=>'类型名字重复'
	            ]);
			}
		}
		
		
        if ($model->load($post) && AdverttypeLogic::instance()->AdverttypeSave($model)) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
                'error'	=> 'ok'
            ]);
        }
		
    }
}